import java.util.List;

public class MyLinkedList implements IList{
    static class ListNode {

        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    //头插
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        if(head == null) {
            head = node;
            last = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    //尾插
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if(head == null) {
            head = node;
            last = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    //中间插入元素
    @Override
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);

        ListNode cur = head;
        if(index < 0 ||index >size()) {
            throw new IndexException("index不合法"+index);
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        int count = 0;
        while(count < index) {
            cur = cur.next;
            count++;
        }
        node.next = cur;
        cur.prev.next = node;
        cur.prev = node;
        node.prev = cur.prev;
    }

    //链表是否包含key这个元素
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除
    @Override
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                //头删
                if (cur == head) {
                    head = head.next;
                    //head的prev处理
                    if (head == null) {
                        //只有一个节点,且是要删的节点
                        last = null;
                    } else {
                        head.prev = null;
                    }

                } else {
                    cur.prev.next = cur.next;
                    //删除中间节点
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        //删除尾巴节点
                        last = last.prev;
                    }
                    return;
                }
                cur = cur.next;
            }
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                //头删
                if (cur == head) {
                    head = head.next;
                    //head的prev处理
                    if (head == null) {
                        //只有一个节点,且是要删的节点
                        last = null;
                    } else {
                        head.prev = null;
                    }

                } else {
                    cur.prev.next = cur.next;
                    //删除中间节点
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        //删除尾巴节点
                        last = last.prev;
                    }
                }
                cur = cur.next;
            }
        }
    }

    //链表大小
    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //清空链表
    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        this.head = null;
        this.last = null;
    }

    //打印链表
    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
